package cl.uchile.sketchpad.web.controllers;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cl.uchile.sketchpad.persistance.model.User;
import cl.uchile.sketchpad.services.SubjectVersionRegistrationService;
import cl.uchile.sketchpad.services.UserService;
import cl.uchile.sketchpad.web.controllers.util.HttpUtil;

@Controller
public class UsersController {

	@Autowired private UserService userService;
	@Autowired private SubjectVersionRegistrationService subjectVersionRegistrationService;

	
	@RequestMapping(value = "/", method = { GET })
	public String index(HttpServletRequest request, Model model) {
		model.addAttribute("hostName", HttpUtil.getHostName(request));
		return "index";
	}
	
	private String loginFenResponse(User user, Model model, HttpServletRequest request) {
		if ( user.getMail() == null 
				|| "".equals(user.getMail()) 
				|| user.getName() == null 
				|| "".equals(user.getName()) ) {
			model.addAttribute("user", user);
			return "firstLoginRegistration";
		} else
			return "redirect:/subjectVersionSelection";
	}
	
	@Transactional
	@RequestMapping(value = "/login-fen", method = { POST , GET })
	public String loginFen(
			HttpServletRequest request,
			Model model,
			@RequestParam(value="username", required=false) String username,
			@RequestParam(value="sid", required=false) String sid,
			@RequestParam(value="from", required=false) String from)
	{
		if ( request.getMethod() == "POST" ) {
			HttpUtil.executePost("http://media.fen.uchile.cl/logmod/verificacion.php?username=" + username + "&sid=" + sid , "username=" + username + "&sid=" + sid );
			
			if ( username == null || "".equals(username) || sid == null || "".equals(sid) )
				return "redirect:/";
			
			User user = this.userService.getFenUser(username);
			
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(-1);
			session.setAttribute("user", user);
			
			return loginFenResponse(user, model, request);
			
		} else {
			User user = this.userService.getUser(request);
			if ( user == null || user.getId() == null )
				return "redirect:/";
			else
				return this.loginFenResponse(user, model, request);
		}
	}
	
	@RequestMapping(value = "/logout", method = { GET})
	public String logout(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		session.setMaxInactiveInterval(-1);
		session.setAttribute("user", null);
		
		return "redirect:/";
	}
	
	@Transactional
	@RequestMapping(value="/firstLoginRegistration", method = { POST })
	public String firstLoginRegistration(
			HttpServletRequest request,
			Model model,
			@RequestParam("name") String name,
			@RequestParam("lastname") String lastname,
			@RequestParam("mail") String mail)
	{
		User user = this.userService.getUser(request);
		if ( user == null || user.getId() == null )
			return "redirect:/";
		
		if ( name == null || "".equals(name) || mail == null || "".equals(mail) ) {
			model.addAttribute("alert",1);
			model.addAttribute("alertdesc","Please fill Name and Email fields.");
			return "firstLoginRegistration";
		}
		
		User foundUser = this.userService.findByMail(mail);
		if ( foundUser != null ) {
			model.addAttribute("alert",1);
			model.addAttribute("alertdesc","This Email address is already registered.");
			return "firstLoginRegistration";
		}
		
		user.setName(name);
		user.setLastname(lastname);
		user.setMail(mail.toLowerCase());
		this.userService.persist(user);
		this.subjectVersionRegistrationService.assignSubjectVersionUsers();

		HttpSession session = request.getSession(false);
		session.setAttribute("user", user);
		model.addAttribute("user", user);
		
		return "redirect:/subjectVersionSelection";
	}
	
}
